﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace DynamicGeometry
{
    public class DilationCreator : FigureCreator
    {

        [PropertyGridName("Dilation Factor")]
        public class Dialog
        {
            
            public Dialog(DilationCreator parent)
            {
                this.parent = parent;
            }

            DilationCreator parent;

            [PropertyGridVisible]
            [PropertyGridFocus]
            [PropertyGridName("Factor = ")]
            public double factor { get; set; }
            
        }

        Dialog dialog;

        public override object PropertyBag
        {
            get
            {
                if (dialog == null)
                {
                    dialog = new Dialog(this);
                }
                return dialog;
            }
        }
        
        protected override DependencyList InitExpectedDependencies()
        {
            return DependencyList.PointPoint;
        }

        protected override IFigure LookForExpectedDependencyUnderCursor(Point coordinates)
        {
            if (FoundDependencies.Count == 0)
            {
                var result = Drawing.Figures.HitTest(coordinates);
                if (result is IPoint || result is ILine || result is ICircle || result is PolygonBase)
                {
                    return result;
                }
            }
            else if (FoundDependencies.Count == 1)
            {
                var result = Drawing.Figures.HitTest(coordinates);
                if (result is IPoint)
                {
                    return result;
                }
            }
            return base.LookForExpectedDependencyUnderCursor(coordinates);
        }

        protected override bool ExpectingAPoint()
        {
            return false;
        }

        protected override void AddFoundDependency(IFigure figure)
        {
            if (figure != null)
            {
                FoundDependencies.Add(figure);
            }
        }

        protected override void CreateAndAddFigure()
        {
            Check.ElementCount(FoundDependencies, 2);
            Check.NoNullElements(FoundDependencies);

                 var results = Transformer.CreateDilatedFigure(
                    Drawing,
                    FoundDependencies[0],
                    FoundDependencies[1],
                    this.dialog.factor);

                Check.NotNull(results);
                Check.NoNullElements(results);

                TempResult = results.Last();
                Drawing.Add(results);
        }

        protected override IFigure CreateFigure()
        {
            DilatedPoint result = Factory.CreateDilatedPoint(Drawing, FoundDependencies, this.dialog.factor);
            return result;
        }

        public override string Name
        {
            get { return "Dilation"; }
        }

        public override string HintText
        {
            get
            {
                return "Enter a factor, click a figure then click a center.";
            }
        }

        public override FrameworkElement CreateIcon()
        {
            return IconBuilder.BuildIcon()
                .Polygon(
                    new SolidColorBrush(Color.FromArgb(255, 128, 255, 128)),
                    new SolidColorBrush(Colors.Black),
                    new Point(0.1, 0.9),
                    new Point(0.9, 0.9),
                    new Point(0.9, 0.1),
                    new Point(0.1, 0.1))
                .Polygon(
                    new SolidColorBrush(Colors.Yellow),
                    new SolidColorBrush(Colors.Black),
                    new Point(0.1, 0.9),
                    new Point(0.5, 0.9),
                    new Point(0.5, 0.5),
                    new Point(0.1, 0.5))
                .Point(0.1, 0.9)
                .Canvas;
        }
    }
}